<template>
  <div class="app-container">
    <div class="search-container">
      <el-form class="form_container" ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="平台" prop="platformId">
          <el-select clearable @keyup.enter="handleQuery" v-model="queryParams.platformId" placeholder="请选择" style="width: 16rem">
            <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道" prop="channelId">
          <el-select clearable @keyup.enter="handleQuery" v-model="queryParams.channelId" placeholder="请选择" style="width: 16rem">
            <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.cityId" placeholder="请选择" style="width: 16rem">
            <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="订单类型" prop="orderType">
          <el-select clearable @keyup.enter="handleQuery" v-model="queryParams.orderType" placeholder="请选择" style="width: 16rem">
            <el-option v-for="item in orderTypeArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="车型" prop="vehicleTypeId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.vehicleTypeId" placeholder="请选择" style="width: 16rem">
            <el-option v-for="item in carTypeArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" @click="handleEdit('新增', {})">
          <template #icon><Plus /></template>
          新增
        </el-button>
      </template>
      <el-table v-loading="loading" :data="TableData" highlight-current-row row-key="id" @row-click="handleRowClick">
        <el-table-column label="平台" min-width="120" align="center">
          <template #default="scope">
            <div>{{ platFormEnum[scope.row.platformId] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="渠道" min-width="120" align="center">
          <template #default="scope">
            <div>{{ channelEnum[scope.row.channelId] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="城市" min-width="120" align="center" prop="cityName"></el-table-column>
        <el-table-column label="订单类型" min-width="120" align="center">
          <template #default="scope">
            <div>{{ orderTypeEnum[scope.row.orderType] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="车型" min-width="80" align="center" prop="vehicleTypeId">
          <template #default="scope">
            <div>{{ carTypeEnum[scope.row.vehicleTypeId] }}</div>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" min-width="120" align="center" prop="createTime"></el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="220">
          <template #default="scope">
            <div style="display: flex; justify-content: center; align-items: center">
              <el-button type="primary" link size="small" @click.stop="handleEdit('编辑', scope.row)">
                <template #icon><Pointer /></template>
                编辑
              </el-button>
              <el-button type="danger" link size="small" @click.stop="handleDel(scope.row)">
                <template #icon><Pointer /></template>
                删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <!-- 改派 -->
    <EditModal ref="editModalRef" @success="handleQuery" />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "SysMenu",
  inheritAttrs: false,
});
import JntApi from "@/api/jnt";
import CommonApi from "@/api/common";
import EditModal from "./components/editModal.vue";
import router from "@/router";
import {
  orderStatusEnum,
  platFormEnum,
  channelEnum,
  carTypeEnum,
  orderTypeEnum,
  bizTypeEnum,
  luggageEnum,
  channelArr,
  bizTypeArr,
  orderTypeArr,
  platFormArr,
  carTypeArr,
} from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";

const queryFormRef = ref(ElForm);
const editModalRef = ref(null);
const total = ref(0);

const loading = ref(false);
const cityOptions = ref();

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);

// 选择表格的行菜单ID
const selectedMenuId = ref<number | undefined>();

function handleDel(row: any) {
  ElMessageBox.confirm("确认删除?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    function () {
      JntApi.priceConfigDel({ id: row.id })
        .then(() => {
          ElMessage.success("删除成功");
          handleQuery();
        })
        .finally(() => (loading.value = false));
    },
    function () {
      ElMessage.info("已取消删除");
    }
  );
}
//编辑触发
function handleEdit(title: string, row: any) {
  console.log(row);
  let data = {
    platformId: row.platformId,
    channelId: row.channelId,
    cityId: row.cityId,
    orderType: row.orderType,
    vehicleTypeId: row.vehicleTypeId,
    id: row.id,
  };
  if (title == "编辑") {
    router.push(`/priceRuleInfo?data=${JSON.stringify(data)}`);
  } else {
    router.push(`/priceRuleInfo`);
  }
}
// 查询菜单
function handleQuery() {
  loading.value = true;
  JntApi.getPriceConfigList(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

// 行点击事件
function handleRowClick(row: MenuVO) {
  // 记录表格选择的菜单ID，新增子菜单作为父菜单ID
  selectedMenuId.value = row.id;
}

onMounted(() => {
  handleQuery();
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
